package com.java8.Lambda;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;

public class demo02_排序 {
    @Test
    public void test01(){
        List<String> fruits = Arrays.asList("cpp", "c", "java", "python");
        fruits.sort((s1,s2)->s1.length()-s2.length());  //升序
        System.out.println(fruits);
    }
    @Test
    public void test02(){
        List<Person> persons = Arrays.asList(
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Charlie", 30),
                new Person("David", 20)
        );
        persons.sort((p1,p2)->p1.age-p2.age);
        System.out.println(persons);
        // 输出: [David (20), Bob (25), Alice (30), Charlie (30)]
    }
}
class Person {
    String name;
    int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String toString() {
        return name + " (" + age + ")";
    }
}